using System;
using System.Collections.Generic;
using System.Text;
using System.Data;           //CommandType
using System.Data.SqlClient; //SqlConnection, SqlCommand, SqlDataReader
using Etna.Exportaciones.En;

namespace Etna.Exportaciones.Ad
{
   public class AdUbigeo
   {
      //public AdUbigeo(SqlConnection con)
      //{
      //   conexion = con;
      //}

       public EnUbigeo Leer(int pidUbigeo, SqlConnection conexion)
      {
         EnUbigeo oEnUbigeo = null;
         SqlCommand cmd = new SqlCommand("sExportaciones_UbigeoLeer", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidUbigeo", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = pidUbigeo;

         SqlDataReader drd = cmd.ExecuteReader(CommandBehavior.SingleResult);
         if (drd != null)
         {

            if (drd.Read())
            {
               oEnUbigeo = new EnUbigeo();
               oEnUbigeo.idUbigeo = drd.GetInt32(0);
               if (!drd.IsDBNull(1))
               {
                  oEnUbigeo.subigeo = drd.GetString(1);
               }
               if (!drd.IsDBNull(2))
               {
                  oEnUbigeo.sDepartamento = drd.GetString(2);
               }
               if (!drd.IsDBNull(3))
               {
                  oEnUbigeo.sProvincia = drd.GetString(3);
               }
               if (!drd.IsDBNull(4))
               {
                  oEnUbigeo.sDistrito = drd.GetString(4);
               }
            }
            drd.Close();
         }

         return oEnUbigeo;
      }

       public bool Insertar(EnUbigeo poEnUbigeo, SqlConnection conexion)
      {
         bool bExito = false;
         SqlCommand cmd = new SqlCommand("sExportaciones_UbigeoInsertar", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piubigeo", SqlDbType.VarChar);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = poEnUbigeo.subigeo;

         SqlParameter Parametro2 = cmd.Parameters.Add("@piDepartamento", SqlDbType.VarChar);
         Parametro2.Direction = ParameterDirection.Input;
         Parametro2.Value = poEnUbigeo.sDepartamento;

         SqlParameter Parametro3 = cmd.Parameters.Add("@piProvincia", SqlDbType.VarChar);
         Parametro3.Direction = ParameterDirection.Input;
         Parametro3.Value = poEnUbigeo.sProvincia;

         SqlParameter Parametro4 = cmd.Parameters.Add("@piDistrito", SqlDbType.VarChar);
         Parametro4.Direction = ParameterDirection.Input;
         Parametro4.Value = poEnUbigeo.sDistrito;

         int n = cmd.ExecuteNonQuery();
         bExito = (n > 0);

         return bExito;
      }


       public bool Modificar(EnUbigeo poEnUbigeo, SqlConnection conexion)
      {
         bool bExito = false;
         SqlCommand cmd = new SqlCommand("sExportaciones_UbigeoModificar", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidUbigeo", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = poEnUbigeo.idUbigeo;

         SqlParameter Parametro2 = cmd.Parameters.Add("@piubigeo", SqlDbType.VarChar);
         Parametro2.Direction = ParameterDirection.Input;
         Parametro2.Value = poEnUbigeo.subigeo;

         SqlParameter Parametro3 = cmd.Parameters.Add("@piDepartamento", SqlDbType.VarChar);
         Parametro3.Direction = ParameterDirection.Input;
         Parametro3.Value = poEnUbigeo.sDepartamento;

         SqlParameter Parametro4 = cmd.Parameters.Add("@piProvincia", SqlDbType.VarChar);
         Parametro4.Direction = ParameterDirection.Input;
         Parametro4.Value = poEnUbigeo.sProvincia;

         SqlParameter Parametro5 = cmd.Parameters.Add("@piDistrito", SqlDbType.VarChar);
         Parametro5.Direction = ParameterDirection.Input;
         Parametro5.Value = poEnUbigeo.sDistrito;

         int n = cmd.ExecuteNonQuery();
         bExito = (n > 0);

         return bExito;
      }

       public List<EnUbigeo> Listar(int pidUbigeo, SqlConnection conexion)
      {
         List<EnUbigeo> loEnUbigeo = null;
         EnUbigeo oEnUbigeo = null;
         SqlCommand cmd = new SqlCommand("sExportaciones_UbigeoListar", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidUbigeo", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = pidUbigeo;

         SqlDataReader drd = cmd.ExecuteReader(CommandBehavior.SingleResult);
         if (drd != null)
         {

            loEnUbigeo = new List<EnUbigeo>();
            while (drd.Read())
            {
               oEnUbigeo = new EnUbigeo();
               oEnUbigeo.idUbigeo = drd.GetInt32(0);
               if (!drd.IsDBNull(1))
               {
                  oEnUbigeo.subigeo = drd.GetString(1);
               }
               if (!drd.IsDBNull(2))
               {
                  oEnUbigeo.sDepartamento = drd.GetString(2);
               }
               if (!drd.IsDBNull(3))
               {
                  oEnUbigeo.sProvincia = drd.GetString(3);
               }
               if (!drd.IsDBNull(4))
               {
                  oEnUbigeo.sDistrito = drd.GetString(4);
               }
               loEnUbigeo.Add(oEnUbigeo);
            }
            drd.Close();
         }

         return loEnUbigeo;
      }
   }
}
